﻿
var Global_Cell_IsStartSelect = false; //是否开始多选
var Global_Cell_StartCellsIndex = -1; //记录选择日期的起始单元格索引
var Global_Cell_EndCellsIndex = -1; //记录选择日期的结束单元格索引
var Global_DateObj; //记录日期控件的主体对象
var Global_CellsObj; //记录单元格对象集合
var Global_Year = 0; //记录全局的年
var Global_Month = 0; //记录全局的月份
var Global_WorkDay_DoMessage; //消息提示容器
var Global_WorkDay_BgColor = ["white", "#C1D9FF", "yellow", "#F28B58"]; //工作日背景颜色,非工作日背景颜色,选中背景颜色,当前日（今天）标注背景颜色

//初始格式化万年历的状态等
function BindFormatDate() {
    //获取对象
    Global_DateObj = $("#cm");
    Global_CellsObj = $("#cm").find(".cell");

    ShowMessage("正在加载状态……");
    $.ajax({
        url: "WorkDay.ashx?t=" + new Date().getTime().toString(),
        type: "post",
        dataType: "json",
        data: { action: "load", wyear: Global_Year, wmonth: Global_Month },
        success: function (data) {
            if (data.state) {
                if (data.rows.length > 0) {
                    for (var i = 0; i < data.rows.length; i++) {
                        Global_CellsObj.eq(parseInt(data.rows[i].Day_day) - 1).attr("WordState", "freeday").attr("bbgColor", Global_WorkDay_BgColor[1]).css("background-color", Global_WorkDay_BgColor[1]);
                    }
                }
                ShowMessage("加载成功！");
            }
            else {
                ShowMessage("加载失败，请刷新浏览器后重试");
            }
        },
        error: function (i, ex) {
            ShowMessage("加载数据出现异常，请刷新浏览器后重试！");
        }
    });
}

//绑定日期单元格DIV的选中事件
function BindCellSelected() {
    //鼠标按下
    Global_CellsObj.live("mousedown", function () {
        Global_Cell_IsStartSelect = true;
        Global_Cell_StartCellsIndex = Global_CellsObj.index($(this));
    });
    //鼠标松开
    $(document).live("mouseup", function () {
        Global_Cell_IsStartSelect = false;
        Global_Cell_StartCellsIndex = -1;
        Global_Cell_EndCellsIndex = -1;
    });
    //鼠标移动事件
    Global_CellsObj.live("mousemove", function () {
        if (Global_Cell_IsStartSelect) {
            if (Global_Cell_EndCellsIndex != Global_CellsObj.index($(this))) {
                Global_Cell_EndCellsIndex = Global_CellsObj.index($(this));
                ClearCellsSelected();
                for (var i = Global_Cell_StartCellsIndex; i <= Global_Cell_EndCellsIndex; i++) {
                    Global_CellsObj.eq(i).attr("IsSelected", true);
                    Global_CellsObj.eq(i).css("background-color", Global_WorkDay_BgColor[2]);
                }
            }
        }
    });
    //点击事件
    $("#cm").find(".cell").live("click", function () {
        //清空选取状态
        ClearCellsSelected();
        //设置点击效果
        $(this).css("background-color", Global_WorkDay_BgColor[2]);
        $(this).attr("IsSelected", true);
        //设定工作日 OR 非工作日
        if ($(this).attr("WordState") == "workday") {
            $("#Rd_WorkDay").attr("checked", "checked");
        }
        else if ($(this).attr("WordState") == "freeday") {
            $("#Rd_FreeDay").attr("checked", "checked");
        }
    });
}

//清空所有的选中状态
function ClearCellsSelected() {
    Global_CellsObj.each(function (i) {
        $(this).css("background-color", $(this).attr("bbgColor"));
        $(this).attr("IsSelected", false);
    });
}

//获取选中的日期值及状态
function GetSelectedCellsValue() {
    var temp_Message = "";
    Global_CellsObj.each(function (i) {
        if ($(this).attr("IsSelected") == "true") {
            temp_Message += $(this).attr("dvalue").toString() + ",";
        }
    });
    if (temp_Message != "") {
        temp_Message = temp_Message.substr(0, temp_Message.length - 1);
    }
    return temp_Message;
}

//显示提示语
function ShowMessage(sMess) {
    Global_WorkDay_DoMessage.text(sMess);
}

//工作日的方法
function WorkDateToSave() {
    var temp_Days = GetSelectedCellsValue();
    var temp_DayState = $(".Buttom_SelectDiv").find("input[type='radio']:checked").val();
    if (temp_Days == "") {
        ShowMessage("请选择日期！");
        return;
    }
    else if (!temp_DayState) {
        ShowMessage("请选择工作日或非工作日！");
        return;
    }
    else {
        Global_WorkDay_DoMessage.text("正在提交请求……");
        $.ajax({
            url: "WorkDay.ashx?t=" + new Date().getTime().toString(),
            type: "post",
            dataType: "json",
            data: { action: "save", wdays: temp_Days, daystate: temp_DayState, wyear: Global_Year, wmonth: Global_Month },
            success: function (data) {
                if (data.state) {
                    //更新众多日期的状态
                    var temp_Arry = temp_Days.split(',');
                    var temp_State = temp_DayState == "0" ? "workday" : "freeday";
                    var temp_BgColor = temp_DayState == "0" ? Global_WorkDay_BgColor[0] : Global_WorkDay_BgColor[1];
                    for (var i = 0; i < temp_Arry.length; i++) {
                        if (Global_CellsObj.eq(parseInt(temp_Arry[i]) - 1).attr("IsToday") == "true") {
                            var temp_TodayBgColor = temp_DayState == "0" ? Global_WorkDay_BgColor[3] : Global_WorkDay_BgColor[1];
                            Global_CellsObj.eq(parseInt(temp_Arry[i]) - 1).attr("WordState", temp_State).attr("bbgColor", temp_TodayBgColor);
                            Global_CellsObj.eq(parseInt(temp_Arry[i]) - 1).css("background-color", temp_TodayBgColor);
                        }
                        else {
                            Global_CellsObj.eq(parseInt(temp_Arry[i]) - 1).attr("WordState", temp_State).attr("bbgColor", temp_BgColor);
                            Global_CellsObj.eq(parseInt(temp_Arry[i]) - 1).css("background-color", temp_BgColor);
                        }
                    }
                    ShowMessage("保存成功！");
                }
                else {
                    ShowMessage("操作失败，请刷新浏览器重试！");
                }
            },
            error: function (i, ex) {
                ShowMessage("出现异常，请刷新浏览器重试！");
            }
        });
    }
}

//获取指定时间范围内的工作日天数
function GetWorkDaysCountByDiff() {
    var temp_DiffStartDate = $("#txt_DiffStartDate");
    var temp_DiffEndDate = $("#txt_DiffEndDate");

    var temp_ShowMessage = "";

    if (temp_DiffStartDate.val() == "") {
        temp_ShowMessage += "请选择起始日期！\n";
    }

    if (temp_DiffEndDate.val() == "") {
        temp_DiffEndDate += "请选择结束日期！\n";
    }

    if (temp_ShowMessage != "") {
        ShowMessage(temp_ShowMessage);
        return;
    }

    $.ajax({
        url: "WorkDay.ashx?t=" + new Date().getTime().toString(),
        type: "post",
        dataType: "json",
        data: { action: "getwcount", sdate: temp_DiffStartDate.val(), edate: temp_DiffEndDate.val() },
        success: function (data) {
            if (data.state) {
                $("#sp_WorkDay").text(data.workday);
                $("#sp_FreeDay").text(data.freeday);
                ShowMessage("获取数据成功！");
            }
            else {
                ShowMessage("获取数据失败，请刷新浏览器的重试！");
            }
        },
        error: function (i, ex) {
            ShowMessage("请求服务器失败，请刷新浏览器后重试！")
        }
    });
}

//绑定各按钮的事件
function BindBtnEvent() {
    //绑定保存按钮的点击方法
    $("#btn_SaveWordDate").bind("click", WorkDateToSave);
    //绑定获取工作日天数按钮的点击方法
    $("#btn_GetDiffDate").bind("click", GetWorkDaysCountByDiff);
}

//页面加载事件
$(document).ready(function () {
    //去掉文字选中效果
    document.body.onselectstart = document.body.ondrag = function () {
        return false;
    }
    //获取消息容器
    Global_WorkDay_DoMessage = $("#WorkDay_DoMessage");
    //绑定初始年、月、值
    $("#top").find("select").each(function () {
        if ($(this).attr("stype") == "year") {
            Global_Year = parseInt($(this).find("option:selected").eq(0).val());
        }
        else if ($(this).attr("stype") == "month") {
            Global_Month = parseInt($(this).find("option:selected").eq(0).val());
        }
    });

    //给年度及月份选择绑定改变事件
    $("#top").find("select").bind("change", function () {
        if ($(this).attr("stype") == "year") {
            Global_Year = parseInt($(this).find("option:selected").eq(0).val());
        }
        else if ($(this).attr("stype") == "month") {
            Global_Month = parseInt($(this).find("option:selected").eq(0).val());
        }
        //格式化状态等
        BindFormatDate();
    });
    //格式化状态等
    BindFormatDate();
    //绑定日期单元格DIV的选中事件
    BindCellSelected();
    //绑定各按钮的事件
    BindBtnEvent();
});